package cz.muni.fi.pa165.business.serviceImpl;

import cz.muni.fi.pa165.api.ForestVisitService;
import cz.muni.fi.pa165.api.dto.ForestDTO;
import cz.muni.fi.pa165.api.dto.ForestVisitDTO;
import cz.muni.fi.pa165.api.dto.PickerDTO;
import cz.muni.fi.pa165.business.dao.ForestVisitDAO;
import cz.muni.fi.pa165.business.service.utils.ForestObjectTransformer;
import cz.muni.fi.pa165.business.service.utils.ForestVisitObjectTransformer;
import cz.muni.fi.pa165.business.service.utils.PickerObjectTransformer;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Matej Cimbora
 */
@Service
public class ForestVisitServiceImpl implements ForestVisitService {

    @Autowired
    private ForestVisitDAO forestVisitDAO;

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    @Transactional
    public void createVisit(ForestVisitDTO forestVisitDTO) {
        forestVisitDAO.createVisit(
                ForestVisitObjectTransformer.forestVisitDTOtoForestVisit(forestVisitDTO));
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public ForestVisitDTO getVisit(Long id) {
        return ForestVisitObjectTransformer.forestVisitToForestVisitDTO(
                forestVisitDAO.getVisit(id));
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Override
    @Transactional
    public void removeVisit(ForestVisitDTO forestVisitDTO) {
        forestVisitDAO.removeVisit(
                ForestVisitObjectTransformer.forestVisitDTOtoForestVisit(forestVisitDTO));
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Override
    @Transactional
    public void updateVisit(ForestVisitDTO forestVisitDTO) {
        forestVisitDAO.updateVisit(
                ForestVisitObjectTransformer.forestVisitDTOtoForestVisit(forestVisitDTO));
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public List<ForestVisitDTO> retrieveAllVisits() {
        return ForestVisitObjectTransformer.forestVisitListToForestVisitListDTO(
                forestVisitDAO.retrieveAllVisits());
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public List<ForestVisitDTO> retrieveVisitsByPicker(PickerDTO pickerDTO) {
        return ForestVisitObjectTransformer.forestVisitListToForestVisitListDTO(
                forestVisitDAO.retrieveVisitsByPicker(
                PickerObjectTransformer.pickerDTOToPicker(pickerDTO)));
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public List<ForestVisitDTO> retrieveVisitsByForest(ForestDTO forestDTO) {
        return ForestVisitObjectTransformer.forestVisitListToForestVisitListDTO(
                forestVisitDAO.retrieveVisitsByForest(
                ForestObjectTransformer.forestDTOToForest(forestDTO)));
    }
}
